using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using I2.Loc;
using UnityEngine;

public class RaceInfoUI : MonoBehaviour
{
	public UILabel speedLabel;

	public UILabel timeLabel;

	public UILabel placeLabel;

	[SerializeField]
	private UILabel circleLabel;

	public float checkpointInfoShowingTime = 4f;

	public List<PlayerRaceUI> checkpointInfo;

	public GameObject samplePlayerInfo;

	public UIGrid parentGrid;

	public GameObject checkpointInfoWindow;

	[SerializeField]
	private GameObject resetCarButton;

	public void Reset()
	{
		HideCheckpointInfo();
		SetSpeed("0");
		SetTime("0");
		SetPlace(string.Empty);
		resetCarButton.SetActive(true);
		parentGrid.sorting = UIGrid.Sorting.None;
		for (int i = 0; i < RaceController.instance.raceResult.Count; i++)
		{
			PlayerRaceUI component = UnityEngine.Object.Instantiate(samplePlayerInfo).GetComponent<PlayerRaceUI>();
			component.gameObject.SetActive(true);
			checkpointInfo.Add(component);
			parentGrid.AddChild(component.transform);
			component.transform.localScale = Vector3.one;
		}
		parentGrid.Reposition();
	}

	public void SetSpeed(string speed)
	{
		speedLabel.text = speed + " m/h";
	}

	public void SetTime(string time)
	{
		timeLabel.text = time;
	}

	public void SetPlace(string place)
	{
		placeLabel.text = place;
	}

	public void SetCircle(string circle)
	{
		circleLabel.text = circle;
	}

	public IEnumerator ShowPassedCheckpoint(RaceCheckPoint checkpoint, int circleIndex)
	{
		if (checkpointInfoWindow != null)
		{
			UpdateCheckpointInfo(checkpoint, circleIndex);
			yield return new WaitForSeconds(checkpointInfoShowingTime);
			HideCheckpointInfo();
		}
	}

	public IEnumerator ShowPassedCheckpoint(RaceFinish checkpoint, int circleIndex)
	{
		if (checkpointInfoWindow != null)
		{
			UpdateCheckpointInfo(checkpoint, circleIndex);
			yield return new WaitForSeconds(checkpointInfoShowingTime);
			HideCheckpointInfo();
		}
	}

	public void UpdateCheckpointInfo(RaceCheckPoint checkpoint, int circleIndex)
	{
		_UpdateCheckpointInfo(checkpoint.CheckpointPassTime, circleIndex);
	}

	public void UpdateCheckpointInfo(RaceFinish checkpoint, int circleIndex)
	{
		_UpdateCheckpointInfo(checkpoint.CheckpointPassTime, circleIndex);
	}

	private void _UpdateCheckpointInfo(List<RaceCheckpointPlayer> checkpointTimes, int circleIndex)
	{
		var passedRiders = from x in checkpointTimes
			where x.circleIndex == circleIndex
			select new
			{
				nickname = x.name,
				time = x.time,
				isLocal = x.isLocal
			};
		var source = RaceController.instance.raceResult.Select((RaceResult x) => new
		{
			playerName = x.name,
			time = -1f,
			isLocal = false
		});
		var list = source.Select(x =>
		{
			float time = -1f;
			bool isLocal = false;
			foreach (var item in passedRiders)
			{
				if (x.playerName.Equals(item.nickname))
				{
					time = item.time;
					isLocal = item.isLocal;
				}
			}
			return new
			{
				nick = x.playerName,
				time = time,
				isLocal = isLocal
			};
		}).ToList();
		list.Sort((a, b) =>
		{
			if (a.time < 0f && b.time < 0f)
			{
				if (a.isLocal)
				{
					return -1;
				}
				if (b.isLocal)
				{
					return 1;
				}
				return a.nick.CompareTo(b.nick);
			}
			if (a.time < 0f)
			{
				return 1;
			}
			if (b.time < 0f)
			{
				return -1;
			}
			return (a.time > 0f && b.time > 0f) ? a.time.CompareTo(b.time) : 0;
		});
		if (checkpointInfo.Count != list.Count)
		{
			ClearCheckpointInfo();
			Reset();
		}
		for (int i = 0; i < checkpointInfo.Count; i++)
		{
			checkpointInfo[i].playerName.text = list[i].nick.Substring(0, Mathf.Min(9, list[i].nick.Length)) + "..";
			checkpointInfo[i].time = list[i].time;
			string text = string.Empty;
			TimeSpan timeSpan;
			if (i == 0)
			{
				timeSpan = TimeSpan.FromSeconds(list[i].time);
				checkpointInfo[i].timeText.color = Color.white;
			}
			else
			{
				timeSpan = TimeSpan.FromSeconds(list[i].time - list[0].time);
				text = "+";
				checkpointInfo[i].timeText.color = Color.red;
			}
			if (timeSpan.Minutes > 0)
			{
				checkpointInfo[i].timeText.text = text + string.Format("{0:D2}:{1:D2}.{2:D2}", timeSpan.Minutes, timeSpan.Seconds, Mathf.RoundToInt(timeSpan.Milliseconds) / 10);
			}
			else if (timeSpan.Milliseconds > 0 || timeSpan.Seconds > 0)
			{
				checkpointInfo[i].timeText.text = text + string.Format("{0:D2}.{1:D2}", timeSpan.Seconds, Mathf.RoundToInt(timeSpan.Milliseconds) / 10);
			}
			else if (list[i].time > 0f)
			{
				checkpointInfo[i].timeText.text = "00.00";
			}
			else
			{
				checkpointInfo[i].timeText.text = "--:--";
				checkpointInfo[i].timeText.color = Color.white;
			}
			if (list[i].isLocal)
			{
				checkpointInfo[i].playerName.color = Color.green;
				checkpointInfo[i].playerName.text = ScriptLocalization.Get("Game_Common/lb_You");
			}
			else
			{
				checkpointInfo[i].playerName.color = Color.white;
			}
		}
		parentGrid.Reposition();
		ShowCheckpointsInfo();
	}

	public void ClearCheckpointInfo()
	{
		checkpointInfo.ForEach(delegate(PlayerRaceUI x)
		{
			UnityEngine.Object.Destroy(x.gameObject);
		});
		checkpointInfo.Clear();
	}

	public void HideCheckpointInfo()
	{
		checkpointInfoWindow.SetActive(false);
	}

	public void ShowCheckpointsInfo()
	{
		checkpointInfoWindow.SetActive(true);
	}

	public void RespawnCar()
	{
		RaceController.instance.Respawn();
	}
}
